package com.itcat.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.PmsProductAttributeCategoryApi;
import com.itcat.mapper.PmsProductAttributeCategoryMapper;
import com.itcat.mapper.PmsProductAttributeMapper;
import com.itcat.mapper.PmsProductCategoryAttributeRelationMapper;

import com.itcat.mapper.PmsProductCategoryMapper;
import com.itcat.mysql.param.PageParam;
import com.itcat.mysql.pojo.PmsProductAttributeCategory;
import com.itcat.mysql.pojo.PmsProductCategoryAttributeRelation;
import com.itcat.mysql.pojo.PmsProductCategoryDto;

import org.apache.dubbo.config.annotation.DubboService;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Jerry Broker
 * @project_name rainbow-mall
 * @create 2022-04-02 23:12
 */
@DubboService
public class PmsProductAttributeCategoryApiImpl implements PmsProductAttributeCategoryApi {

    @Autowired
    private PmsProductAttributeCategoryMapper pmsProductAttributeCategoryMapper;

    @Autowired
    private PmsProductAttributeMapper pmsProductAttributeMapper;

    @Autowired
    private PmsProductCategoryMapper pmsProductCategoryMapper;
    @Autowired
    private PmsProductCategoryAttributeRelationMapper pmsProductCategoryAttributeRelationMapper;

    /**
     * 分页查询商品属性分类
     *
     * @param pageParam
     * @return
     */
    @Override
    public Page<PmsProductAttributeCategory> categoryList(PageParam pageParam) {
        Page<PmsProductAttributeCategory> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        pmsProductAttributeCategoryMapper.selectPage(page, null);
        return page;
    }

    /**
     * 添加商品查询分类
     *
     * @param
     */
    @Override
    public void savez(PmsProductCategoryDto pmsProductCategoryDto) {
        pmsProductCategoryMapper.insert(pmsProductCategoryDto);
        List<Integer> ids = pmsProductCategoryDto.getProductAttributeIdList();
        PmsProductCategoryAttributeRelation pmsProductCategoryAttributeRelation = new PmsProductCategoryAttributeRelation();
        pmsProductCategoryAttributeRelation.setProductCategoryId(pmsProductCategoryDto.getId());
        for (Integer id : ids) {
            pmsProductCategoryAttributeRelation.setProductAttributeId(id);
            pmsProductCategoryAttributeRelationMapper.insert(pmsProductCategoryAttributeRelation);
        }
    }

    /**
     * 删除商品属性分类
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        pmsProductAttributeCategoryMapper.deleteById(id);
    }


    /**
     * 查询所有的商品属性分类
     *
     * @return
     */
    @Override
    public List<PmsProductAttributeCategory> listAll() {
        List<PmsProductAttributeCategory> list = pmsProductAttributeCategoryMapper.selectList(null);
        return list;
    }

    /**
     * 更新商品属性分类
     *
     * @param pmsProductAttributeCategory
     */
    @Override
    public void update(PmsProductAttributeCategory pmsProductAttributeCategory) {
        pmsProductAttributeCategoryMapper.updateById(pmsProductAttributeCategory);
    }

    /**
     * 根据id获取商品属性分类
     *
     * @param id
     * @return
     */
    @Override
    public PmsProductAttributeCategory getById(Integer id) {
        PmsProductAttributeCategory select = pmsProductAttributeCategoryMapper.selectById(id);

        return select;
    }

    @Override
    public void save(PmsProductAttributeCategory pmsProductAttribute) {
        int insert = pmsProductAttributeCategoryMapper.insert(pmsProductAttribute);
    }
}
